<?php


namespace app\common\model;

use think\Exception;
use think\Model;

class DayInventory extends Model
{

    /**
     * [stock 单日库存是否充足]
     * @Author   ZhouYunpeng
     * @DateTime 2021-04-29
     * @param    array       $params [description]
     * @return   [type]              [description]
     */
    public function stock(array $params)
    {   

        $goods_id  = $params['goods_id'];
        $num       = $params['num'];
        $play_time = strtotime($params['date']);
        $day = $this->where(['goods_id'=>$goods_id,'day_time'=>$play_time,'oneStock'=>['>',0]])->find();
        if ($day) {
                if (($day['already_sold']+$num) > $day['oneStock']) {
                   return false;
                }else{
                   return true;
                }
        }else{
            return true;
        }

    }

    /***
     * @param $sale_id
     * @param $num
     * @return bool
     * @throws \think\exception\DbException
     * 销售方案库存
     */
    public function salesStock($sale_id, $num)
    {
        $sales = Sales::get($sale_id);

        if ($sales['DayStock'] > 0 &&  $sales['DayStock'] >= $num) {
            return true;
        } elseif ($sales['DayStock'] == -1) {
            return true;
        }
        return false;
    }


    protected function add($sales = [],$sold_num,$time)
    {
        if ($sales['DayStock'] && $sales['DayStock'] > 0) {
            $stock = $sales['DayStock'] - $sold_num;
        } elseif ($sales['DayStock'] && $sales['DayStock'] == -1) {
            $stock = $sales['DayStock']; //库存无限量
        }
        $data = [
            'goods_id'     => $sales['goods_id'],
            'store_id'     => $sales['store_id'],
            'sales_id'     => $sales['id'],
            'day_time'     => $time,
            'cost_price'   => $sales['costPrice'],
            'sale_price'   => $sales['salePrice'],
            'retail_price' => $sales['retailPrice'],
            'oneStock'     => $stock,
            'already_sold' => $sold_num,
            'create_time'  => time(),
        ];

        if($sales['is_confirm']){
            list($stock_sold,$confirm_sold) = $this->isConfirm($sales['stock'],null,$sold_num);
            $data['stock_sold_count'] = $stock_sold;
            $data['confirm_stock_sold_count'] = $confirm_sold;
        }
        $this->allowField(true)->save($data);
    }

    /***
     * @param $stock_count 免库存数量
     * @param $sold_count  免库存销售数量
     * @param $number  该用户购买数量
     * @return int|mixed $value <= 0 免库存，$value > 0 需确认库存数量
     * @throws Exception
     * 是否确认库存
     */
    public function isConfirm($stock_count,$sold_count,$number)
    {
        $confirm_sold = 0;
        if ($sold_count == null){
            $value = $stock_count - $number;
           if ($value >= 0){
               $stock_sold = $number;//免确认库存销售数量
           }elseif ($value < 0){
                $confirm_sold = $number - $stock_count; //需确认库存销售数量
                $stock_sold = $stock_count;
            }
        }else{
            $surplus = $this->surplusStock($stock_count,$sold_count);
            $value = $number - $surplus;//免确认库存的剩余数量与购买票数的差，差值小于等于0免确认库存售完，大于0也有可能包含免确认库存的数量
            if ($value <= 0){
                $stock_sold = $number;
                $confirm_sold = 0;
            }else{
                $confirm_sold = $value;
                $stock_sold   = $number - $confirm_sold;//免确认库存的销售数量
            }

        }
        return [$stock_sold,$confirm_sold];
    }

    /***
     * @param $stock_count 免库存数量
     * @param $sold_count  免库存销售的数量
     * @return mixed 0：已售完，>0 剩余库存数量
     * @throws Exception
     * 计算剩余库存
     */
    private function surplusStock($stock_count,$sold_count)
    {
        $value = $stock_count - $sold_count;
        if ($value < 0){
            throw new Exception('剩余库存不能小于0');
        }
        return $value;
    }



}